/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.threading;

import domain.DomainController;
import domain.forumdata.Forum;
import domain.forumdata.Post;
import java.util.logging.Level;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class Poster extends Thread
{
    private Post post;
    private Forum forum;
    private double adder;

    /**
     * Default constructor.
     * @param post The post object.
     * @param forum The forum to post to.
     * @param adder when completed, add this value to the progress indicator.
     */
    public Poster(Post post, Forum forum, double adder)
    {
        this.post = post;
        this.forum = forum;
        this.adder = adder;
    }

    /**
     * Executes the thread.
     */
    @Override
    public void run()
    {
        DomainController domainController = DomainController.getInstance();
        try
        {
            if(this.forum.hasCategory(this.post.getCat()))
            {
                //First check the cool down queue.
                //Hold posting if this forum is in the queue.
                while(DomainController.getInstance().getCoolDownQueue().isForumInQueue(this.forum))
                {
                    Thread.sleep(1000);
                }

                domainController.getLogger().log(Level.INFO, "Posting to {0}...", this.forum.getForumName());

                int attempt = 1;
                int retries = domainController.getPreferences().getRetry();
                while(attempt <= retries &&
                        !this.forum.doPost(this.post.getCat(),
                                    this.post.getTopicTitle(),
                                    this.post.getTopicSubtitle(),
                                    this.post.getTag(),
                                    this.post.getPost()))
                {
                    //Posting failed, retry.
                    attempt++;
                    Thread.sleep(5000);
                    if(attempt <= retries)
                        domainController.getLogger().log(Level.INFO, "Attempt {0}: posting to {1}...", new String[]{Integer.toString(attempt), this.forum.getForumName()});
                }
                
                //Posting to this forum has been completed,
                //now add it to the cool down queue.
                DomainController.getInstance().getCoolDownQueue().addForumToQueue(this.forum);
            }
            else
            {
                domainController.getLogger().log(Level.WARNING, "Category \"{0}\" has not been assigned to forum \"{1}\"! Ignoring this forum...", new Object[]{this.post.getCat().getName(), this.forum.getForumName()});
            }

            domainController.getLogger().log(Level.INFO, "Posting to {0} completed.", this.forum.getForumName());
            domainController.getUi().addProgress((int)this.adder);
        }
        catch(Exception e)
        {
            domainController.getLogger().log(Level.SEVERE, e.getMessage());
        }
    }
}